/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.parser;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Base service parser for parsing the XML received from the .NET service handler web application.  This class defines all of the 
 * XML elements that will be present in the incoming feeds and generates the URL with the appropriate querystring parameters.
 * @author Kyle Parker, University Computing Services
 */
public abstract class BaseServiceParser implements ServiceParser {
	static final String CATEGORY = "category";
	static final String CONNECTION = "connection";
	static final String CONTENT = "content";
	static final String COURSE = "course";
	static final String DEGREE = "degree";
	static final String EVENT = "event";
	static final String EVENT_TYPE = "eventType";
	static final String HOUSING = "housing";
	static final String ORGANIZATION = "organization";
	static final String PERSON = "person";
	static final String ROOT = "root";
	static final String SI_SESSION = "siSession";
	static final String TERM = "term";
	static final String TUTOR = "tutor";
	
	static final String ADDRESS = "address";
	static final String ADDRESS_1 = "address1";
	static final String ADDRESS_2 = "address2";
	static final String ADVISING_CENTER = "advisingCenter";
	static final String ADVISOR_NAME = "advisorName";
	static final String ADVISOR_TYPE = "advisorType";
	static final String AUDIENCE = "audience";
	static final String BEGIN_DATE = "beginDate";
	static final String BEGIN_TIME = "beginTime";
	static final String BODY = "body";
	static final String BSU_ID = "bsuID";
	static final String BUILDING_CODE = "buildingCode";
	static final String BUILDING_DESC = "buildingDesc";
	static final String CALENDAR_EVENT_ID = "calendarEventID";
	static final String CALENDAR_EVENT_NAME = "calendarEventName";
	static final String CATEGORY_ID = "categoryID";
	static final String CATEGORY_NAME = "categoryName";
	static final String CITY = "city";
	static final String COMMENTS = "comments";
	static final String COMPLEX_DESC = "complexDesc";
	static final String COMPLEX_FEATURES = "complexFeatures";
	static final String COMPLEX_NAME = "complexName";
	static final String CONNECTION_TYPE_ID = "connectionTypeID";
	static final String CONNECTION_TYPE_NAME = "connectionTypeName";
	static final String CONTACT_EMAIL = "contactEmail";
	static final String CONTACT_NAME = "contactName";
	static final String CONTACT_PHONE = "contactPhone";
	static final String CONTENT_ID = "contentID";
	static final String CONTRACT_TYPE = "contractType";
	static final String COST = "cost";
	static final String COURSE_COUNT = "courseCount";
	static final String COURSE_DESC = "courseDesc";
	static final String COURSE_EMAIL = "courseEmail";
	static final String COURSE_INFO = "courseInfo";
	static final String COURSE_NUM = "courseNum";
	static final String COURSE_REF_NUM = "courseRefNum";
	static final String COURSE_WITH_TITLE = "courseWithTitle";
	static final String COURSE_URL = "courseURL";
	static final String CREDIT_TYPE = "creditType";
	static final String CR_NC_A = "crnca";
	static final String DAY_TIME = "dayTime";
	static final String DEGREE_TYPE = "degreeType";
	static final String DUES = "dues";
	static final String DEPARTMENT = "department";
	static final String DISCIPLINE_CODE = "discCode";
	static final String DISPLAY_NAME = "displayName";
	static final String EMAIL_ADDRESS = "emailAddress";
	static final String END_DATE = "endDate";
	static final String END_TIME = "endTime";
	static final String ERROR = "error";
	static final String EVENT_COUNT = "eventCount";
	static final String EVENT_DESC = "eventDesc";
	static final String EVENT_NAME = "eventName";
	static final String FIRST_NAME = "firstName";
	static final String FINAL_EXAM = "finalExam";
	static final String FLOOR_CODE = "floorCode";
	static final String FLOOR_DESC = "floorDesc";
	static final String FOUNDED = "founded";
	static final String FULL_STORY_URL = "fullStoryURL";
	static final String FULL_TITLE = "fullTitle";
	static final String HALL_CODE = "hallCode";
	static final String HALL_DESC = "hallDesc";
	static final String HALL_GROUP_DESC = "hallGroupDesc";
	static final String HEADLINE = "headline";
	static final String HOURS = "hours";
	static final String HOUSING_TYPE = "housingType";
	static final String IMAGE_URL = "imageURL";
	static final String LAST_NAME = "lastName";
	static final String LATITUDE = "latitude";
	static final String LIVING_LEARNING = "livingLearning";
	static final String LOCATION = "location";
	static final String LOCATION_ID = "locationID";
	static final String LOCATION_URL = "locationURL";
	static final String LOGIN_GUID = "loginGuid";
	static final String LONGITUDE = "longitude";
	static final String MEAL_PLAN = "mealPlan";
	static final String MEMBER_COUNT = "memberCount";
	static final String MEMBER_ROLE = "memberRole";
	static final String MEMBER_ROLE_ID = "memberRoleID";
	static final String MEMBER_TYPE_ID = "memberTypeID";
	static final String MEMBERSHIP_REQ = "membershipReq";
	static final String MESSAGE_ID = "messageID";
	static final String MIDDLE_NAME = "middleName";
	static final String NAME_PREFIX = "namePrefix";
	static final String NAME_SUFFIX = "nameSuffix";
	static final String OFFICE = "office";
	static final String ORGANIZATION_DESC = "organizationDesc";
	static final String ORGANIZATION_ID = "organizationID";
	static final String ORGANIZATION_NAME = "organizationName";
	static final String PERSON_TYPE = "personType";
	static final String PHONE = "phone";
	static final String PRIVACY_RESTRICTION = "privacyRestriction";
	static final String PROGRAM_OPTION = "programOption";
	static final String PROGRAM_TYPE = "programType";
	static final String RATE = "rate";
	static final String REPEAT = "repeat";
	static final String ROLE = "role";
	static final String ROOM = "room";
	static final String ROOM_DESC = "roomDesc";
	static final String ROOM_ID = "roomID";
	static final String ROOM_NUMBER = "roomNumber";
	static final String SECTION = "section";
	static final String SHARE_URL = "shareURL";
	static final String SHORT_ACADEMIC_YEAR = "shortAcademicYear";
	static final String SHORT_TERM_FULL_DESC = "shortTermFullDesc";
	static final String STARRED = "starred";
	static final String START_DATE = "startDate";
	static final String STATE = "state";
	static final String SUCCESS = "success";
	static final String TAGLINE = "tagline";
	static final String TERM_BEGIN_DATE = "termBeginDate";
	static final String TERM_END_DATE = "termEndDate";
	static final String TERM_FULL_DESC = "termFullDesc";
	static final String TERM_ID = "termID";
	static final String TERM_NUMBER = "termNumber";
	static final String TERM_YEAR = "termYear";
	static final String THUMB_URL = "thumbURL";
	static final String TITLE = "title";
	static final String TITLE_RANK = "titleRank";
	static final String TOTAL_CREDIT_HOURS = "totalCreditHours";
	static final String UNIVERSITY_CALENDAR_ID = "universityCalendarID";
	static final String URL = "url";
	static final String WEBSITE = "website";
	static final String ZIP = "zip";
	
	private final URL url;

	/**
	 * Generate the URL to be used by the getInputStream method
	 * @param url
	 * @param querystringParms
	 */
	protected BaseServiceParser(String url, List<String> querystringParms) {
		try {
			if (querystringParms.size() > 0) {
                for (int i = 0; i < querystringParms.size(); i++) {
                	url = url + querystringParms.get(i).trim().replace(' ', '+') + "&";
                }
			}
			
			// Remove the trailing & from the querystring
			//url = url.substring(0, url.length() - 1);
			url += "src=android";
			
			this.url = new URL(url);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Open a connection to the server and get the XML stream from the service handler
	 * @return url.openConnection().getInputStream();
	 */
	protected InputStream getInputStream() {
		try {
			return url.openConnection().getInputStream();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}